package com.hdsx.jxcsxm.xtgl.controller;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.hdsx.jxcsxm.common.CommonActionSupport;
import com.hdsx.jxcsxm.utile.*;
import com.hdsx.jxcsxm.xtgl.bean.*;
import com.opensymphony.xwork2.ActionContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

import com.hdsx.jxcsxm.xtgl.server.XtglServer;
import com.hdsx.jxcsxm.utile.http.HttpClient;

/**
 * 系统管理Controller层
 * @author qinwh
 *
 */
@Scope("prototype")
@Controller
public class XtglController extends CommonActionSupport {

	private static final long serialVersionUID = -1512493918772500846L;

	private static final String SM_KEY = "abcdefzhanxyygf";

	private int page = 1;
	private int rows = 10;
	@Resource(name = "xtglServerImpl")
	private XtglServer xtglServer;
	//单位实体
	private Unit unit;
	private Param param;
	private Lxsh lxsh = new Lxsh();
	private Plan_flwbzbz flwbzbz;
	//用户实体
	private Master master;
	private String anyXml;
	private String yhm;
	private String yhzt;
	private String yhdw;
	private String yhids;
	private String username;
	private String password;
	private String gmgid;
	private String id;
	private String yzm;
	private String parent;
	private String wh;
	private String type;
	private String gydw;
	private String xzqh;
	private String sql;
	private String nameValue;
	private String colValue;
	private String nf;
	private String yf;
	private String bbmc;
	private String truename;
	private String xmnf;
	private String gydwbm;
	private String polygon;

	public String getPolygon() {
		return polygon;
	}

	public void setPolygon(String polygon) {
		this.polygon = polygon;
	}

	public String getXmnf() {
		return xmnf;
	}

	public void setXmnf(String xmnf) {
		this.xmnf = xmnf;
	}

	public String getGydwbm() {
		return gydwbm;
	}

	public void setGydwbm(String gydwbm) {
		this.gydwbm = gydwbm;
	}

	public String getBbmc() {
		return bbmc;
	}
	public void setBbmc(String bbmc) {
		this.bbmc = bbmc;
	}
	public XtglServer getXtglServer() {
		return xtglServer;
	}
	public void setXtglServer(XtglServer xtglServer) {
		this.xtglServer = xtglServer;
	}
	public String getGydw() {
		return gydw;
	}
	public void setGydw(String gydw) {
		this.gydw = gydw;
	}
	public String getXzqh() {
		return xzqh;
	}
	public void setXzqh(String xzqh) {
		this.xzqh = xzqh;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public String getNameValue() {
		return nameValue;
	}
	public void setNameValue(String nameValue) {
		this.nameValue = nameValue;
	}
	public String getColValue() {
		return colValue;
	}
	public void setColValue(String colValue) {
		this.colValue = colValue;
	}
	public String getWh() {
		return wh;
	}
	public void setWh(String wh) {
		this.wh = wh;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getParent() {
		return parent;
	}
	public void setParent(String parent) {
		this.parent = parent;
	}
	public String getNf() {
		return nf;
	}
	public void setNf(String nf) {
		this.nf = nf;
	}
	public String getYf() {
		return yf;
	}
	public void setYf(String yf) {
		this.yf = yf;
	}

	public Lxsh getLxsh() {
		return lxsh;
	}
	public void setLxsh(Lxsh lxsh) {
		this.lxsh = lxsh;
	}


	public String getTruename() {
		return truename;
	}
	public void setTruename(String truename) {
		this.truename = truename;
	}
	/**
	 * 重置密碼
	 */
	public void czPassword(){
		MessageDigest md5;
		String temp="";
		try {
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base = new BASE64Encoder();
			temp = base.encode(md5.digest("000000".getBytes()));
			this.master.setPassword(temp);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		boolean bl = xtglServer.czPassword(master);
		if(bl){
			ResponseUtils.write(getresponse(), "true");
		}
		else
			ResponseUtils.write(getresponse(), "false");
	}
	/**
	 * 单位级联查询
	 */
	public void seldw2(){
		try {
			List<TreeNode> l=xtglServer.selchildedw(yhdw+"__");
			String s=JSONArray.fromObject(l).toString();
			ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void seldw(){
		try {
			List<TreeNode> l=xtglServer.seldw(yhdw);
			String s=JSONArray.fromObject(l).toString();
			ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * updatePassword
	 */
	public void updatePassword(){
		String smpass = "";
		String pass = "";
		try {
			SM4Utils sm4Utils = new SM4Utils();
			// 加密
			// smpass = sm4Utils.encode(password, SM_KEY);
			// 解密
			pass = sm4Utils.decode(password, SM_KEY);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap hm=new HashMap();
		hm.put("id", username);
		hm.put("smpass", password);
		hm.put("pass", pass);
		boolean bl = xtglServer.updatePassword(hm);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void updateName(){

		HashMap hm=new HashMap();
		hm.put("id", username);
		hm.put("mm", password);
		boolean bl = xtglServer.updateName(hm);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void isLoginByEncryption(){
		HashMap hm = new HashMap();
		hm.put("truename", master.getTruename());
		final HashMap<String, String> bl = xtglServer.isLoginByEncryption(hm);
		if (bl != null) {
			try {
				JsonUtils.write(bl, getresponse().getWriter());
			} catch (Exception e) {
				ResponseUtils.write(getresponse(), "false");
				e.printStackTrace();
			}
		} else {
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void loginForSM4() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String code = (String) session.getAttribute("code");
		if(code != null && !code.equals(master.getCode()) && !"AAAA".equals(master.getCode())){
			ResponseUtils.write(getresponse(), "false");
			return;
		}

		MessageDigest md5;
		String temp = "";
		try {
			SM4Utils sm4Utils = new SM4Utils();
			temp = sm4Utils.encode(master.getPassword(), SM_KEY);
		} catch (Exception e) {
			e.printStackTrace();
		}
		HashMap hm = new HashMap();
		hm.put("truename", master.getTruename());
		hm.put("password", master.getPassword());
		HashMap<String,String> bl = xtglServer.loginForSM(hm);
		if(bl!=null){
			session.setAttribute("truename", bl.get("TRUENAME"));
			session.setAttribute("unit", bl.get("UNIT"));
			try {
				JsonUtils.write(bl, getresponse().getWriter());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else {
			ResponseUtils.write(getresponse(), "false");
		}

	}

	/**
	 * 登陆
	 */
	public void login(){

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String code=(String) session.getAttribute("code");

		if(!code.equals(master.getCode()) && !"AAAA".equals(master.getCode())){
			ResponseUtils.write(getresponse(), "false");
			return;
		}

		MessageDigest md5;
		String temp="";
		try {
			BASE64Decoder decoder=new BASE64Decoder();
			try {
				master.setPassword(new String(decoder.decodeBuffer(master.getPassword())));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base = new BASE64Encoder();
			String passw=master.getPassword();

			if("A".equals(passw.substring(0,1)) ){passw=passw.substring(1,11);}
			else if("B".equals(passw.substring(0,1)) ){passw=passw.substring(1,12);}
			else if("C".equals(passw.substring(0,1)) ){passw=passw.substring(1,13);}
			else if("D".equals(passw.substring(0,1)) ){passw=passw.substring(1,14);}
			else if("E".equals(passw.substring(0,1)) ){passw=passw.substring(1,15);}
			else if("F".equals(passw.substring(0,1)) ){passw=passw.substring(1,16);}
			else if("G".equals(passw.substring(0,1)) ){passw=passw.substring(1,17);}
			else {passw=passw.substring(1, Integer.valueOf(master.getPassword().substring(0,1)).intValue()+1);}

			temp = base.encode(md5.digest(
					passw.getBytes()
					));

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		HashMap hm=new HashMap();
		hm.put("truename", master.getTruename());
		hm.put("password", temp);
		HashMap<String,String> bl = xtglServer.login(hm);
		if(bl!=null){

			session.setAttribute("truename", bl.get("TRUENAME"));
			try {
				JsonUtils.write(bl, getresponse().getWriter());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}



	public void getCode(){
		String sCode="";
		for (int i = 0; i <= 3; i++) {
			int max=9;
			int min=0;
			Random random = new Random();
			int s = random.nextInt(max)%(max-min+1) + min;
			sCode=sCode+s+"";
		}

			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			session.setAttribute("code", sCode);

				try {
					ResponseUtils.write(getresponse(), sCode);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

	}

	public void loginCheck(){
		HashMap hm=new HashMap();
		hm.put("truename", master.getTruename());
		HashMap<String,String> bl = xtglServer.loginCheck(hm);
		if(bl!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			session.setAttribute("truename", bl.get("TRUENAME"));
			try {
				JsonUtils.write(bl, getresponse().getWriter());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	/**
	 * 行政区划级联查询
	 */
	public void selAllXzqh2() {
		try {
		    List<TreeNode> l=xtglServer.selAllXzqh2(yhdw);
            String s=JSONArray.fromObject(l).toString();
			ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//查询行政区划
	public void selAllXzqh() {
		try {
            List<TreeNode> l=xtglServer.selAllXzqh(yhdw);
		    String s=JSONArray.fromObject(l).toString();
            ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 用户退出系统时删除session信息
	 * @return
	 */
	public void clearSession(){
		System.out.println("清除Session中......");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session.getAttribute("truename") != null){
			session.removeAttribute("truename");
			/*session.setAttribute("truename",null);*/
		}
		ResponseUtils.write(getresponse(), "true");
	}

	/**
	 * 从session中取出用户名查询权限（防止用户没有登录直接访问index页面）
	 */
	public void selQx(){

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String username = (String)session.getAttribute("truename");
		if(username == null || username == ""){
			ResponseUtils.write(getresponse(), "false");
		}else{
			ResponseUtils.write(getresponse(), "true");
		}
	}
	/**
	 * 用户管理列表
	 */
	public void selectYhList(){
		HashMap hm=new HashMap();
		hm.put("page", page);
		hm.put("rows", rows);
		hm.put("yhm", master.getTruename());
		if(!"36".equals(master.getUnit())) hm.put("unit", master.getUnit());
		int count=xtglServer.selectYhListCount(hm);
		List<Master> list=xtglServer.selectYhList(hm);
		EasyUIPage<Master> e=new EasyUIPage<Master>();
		e.setRows(list);
		e.setTotal(count);
		try {
			JsonUtils.write(e, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	/**
	 * 选择角色列表
	 */
	public void selQxList(){
		HashMap hm=new HashMap();
		List<HashMap> list=xtglServer.selQxList(hm);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	/**
	 * 添加用户
	 */
	public void insertYh(){
		MessageDigest md5;
		try {
			XtglParam xtglParam = new XtglParam();
			String roleId = getRoleByCookie();
			System.out.println("roleId = " + roleId);
			if (StringUtils.isBlank(roleId)) {
				xtglParam.setSuccess(false);
				xtglParam.setMessage("您没有添加用户的权限！");
				JsonUtils.write(xtglParam, getresponse().getWriter());
				return;
			}
			xtglParam.setRoleId(roleId);
			xtglParam.setSourceId("01010704");
			boolean bl = xtglServer.querySouceidByRoleId(xtglParam);
			if (!bl) {
				xtglParam.setSuccess(false);
				xtglParam.setMessage("您没有添加用户的权限！");
				JsonUtils.write(xtglParam, getresponse().getWriter());
				return;
			}
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base = new BASE64Encoder();
			String temp = base.encode(md5.digest(master.getPassword().getBytes()));
			master.setPassword(temp);
			boolean b = xtglServer.insertYh(master);
			if (b != true) {
				xtglParam.setSuccess(false);
				// ResponseUtils.write(getresponse(), "false");
			} else {
				xtglParam.setSuccess(true);
				// ResponseUtils.write(getresponse(), "true");
			}
			JsonUtils.write(xtglParam, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 添加用户
	 */
	public void updatezt(){
		HashMap hm=new HashMap();
		hm.put("id", yhm);
		hm.put("zt", yhzt);
		boolean bl = xtglServer.updatezt(hm);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}
	/**
	 * 重置密码
	 */
	public void mimareset(){
		MessageDigest md5;
		String temp="";
		try {
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base = new BASE64Encoder();
			temp = base.encode(md5.digest("000000".getBytes()));

		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap hm=new HashMap();
		hm.put("id", yhm);
		hm.put("password", temp);
		boolean bl = xtglServer.mimareset(hm);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}
	/**
	 * 删除用户
	 */
	public void deleteYh(){
		try {
			String roleId = getRoleByCookie();
			XtglParam xtglParam = new XtglParam();
			if (StringUtils.isBlank(roleId)) {
				xtglParam.setSuccess(false);
				xtglParam.setMessage("您没有添加用户的权限！");
				JsonUtils.write(xtglParam, getresponse().getWriter());
				return;
			}
			xtglParam.setRoleId(roleId);
			xtglParam.setSourceId("01010704");
			boolean bl = xtglServer.querySouceidByRoleId(xtglParam);
			if (!bl) {
				xtglParam.setSuccess(false);
				xtglParam.setMessage("您没有添加用户的权限！");
				JsonUtils.write(xtglParam, getresponse().getWriter());
				return;
			}
			boolean bls = xtglServer.deleteYh(yhm);
			if (bls == true) {
				xtglParam.setSuccess(true);
				JsonUtils.write(xtglParam, getresponse().getWriter());
				// ResponseUtils.write(getresponse(), "true");
			}else{
				xtglParam.setSuccess(false);
				JsonUtils.write(xtglParam, getresponse().getWriter());
				// ResponseUtils.write(getresponse(), "false");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除用户
	 */
	public void updateYh(){
		/*
		MessageDigest md5;
		String temp="";
		try {
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base = new BASE64Encoder();
			temp = base.encode(md5.digest("000000".getBytes()));
			master.setPassword(temp);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}*/
		boolean bl = xtglServer.updateYh(master);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}
	/**
	 * 角色列表
	 */
	public void selectJsList(){
		List<Param> list=xtglServer.selectJsList(param);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	/**
	 * 角色列表
	 */
	public void insertJs(){
		try {
			String roleId = getRoleByCookie();
			XtglParam xtglParam = new XtglParam();
			if (StringUtils.isBlank(roleId)) {
				xtglParam.setSuccess(false);
				xtglParam.setMessage("您没有添加用户的权限！");
				JsonUtils.write(xtglParam, getresponse().getWriter());
				return;
			}
			xtglParam.setRoleId(roleId);
			xtglParam.setSourceId("01010701");
			boolean bl = xtglServer.querySouceidByRoleId(xtglParam);
			if (!bl) {
				xtglParam.setSuccess(false);
				xtglParam.setMessage("您没有添加用户的权限！");
				JsonUtils.write(xtglParam, getresponse().getWriter());
				return;
			}
			boolean bls = xtglServer.insertJs(param);
			if (bls) {
				xtglParam.setSuccess(true);
				JsonUtils.write(xtglParam, getresponse().getWriter());
				// ResponseUtils.write(getresponse(), "true");
			} else {
				xtglParam.setSuccess(false);
				JsonUtils.write(xtglParam, getresponse().getWriter());
				// ResponseUtils.write(getresponse(), "false");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateJs(){
		boolean bl  = xtglServer.updateJs(param);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void selectJsById(){
		Param p=xtglServer.selectJsById(param);
		try {
			JsonUtils.write(p, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 删除角色
	 */
	public void deleteJsById(){
		boolean bl  = xtglServer.deleteJsById(param);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	/**
	 * 行政区划树列表
	 */
	public void selectXzqhList(){
		try {
		int count =0;
		List<Unit> list=new ArrayList<Unit>();

		if(id==null){
			count = xtglServer.selectXzqhListCount(unit);
			list = xtglServer.selectXzqhList(unit);
			int len=unit.getId().length();
			for(int i=0;i<list.size();i++){
				if(!unit.getId().equals(list.get(i).getId())&&i!=0)
				{
					list.get(i).set_parentId(list.get(i).getParent());
				}
				if(list.get(i).getBmid().length()>=(len/2)*6+6||len==6){
					list.get(i).setState("closed");
				}
			}
		}else{
			list = xtglServer.selectLxQlDataList(id);
			for(int i=0;i<list.size();i++){
				list.get(i).set_parentId(list.get(i).getParent());
				list.get(i).setState("closed");
			}
		}

		EasyUIPage<Unit> ep = new EasyUIPage<Unit>();
		ep.setTotal(count);
		ep.setRows(list);
		JsonUtils.write(ep, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 单位树列表
	 */
	public void selectDwList(){
		int count = xtglServer.selectDwListCount(unit);
		List<Unit> list = xtglServer.selectDwList(unit);
		int len=unit.getId().length();
		if(len!=0) len+=4;
		for(int i=0;i<list.size();i++){
			if(!unit.getId().equals(list.get(i).getId())&&i!=0)
			{
				list.get(i).set_parentId(list.get(i).getParent());
			}
			if(list.get(i).getUnit().length()>len+11){
				list.get(i).setState("closed");
			}
		}
		EasyUIPage<Unit> ep = new EasyUIPage<Unit>();
		ep.setTotal(count);
		ep.setRows(list);
		try {
			JsonUtils.write(ep, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 编码编码树列表
	 */
	public void getBmbmTreeByName(){
		try {
			yhm=java.net.URLDecoder.decode(yhm,"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		HashMap<String, String> hm=new HashMap<String, String>();
		hm.put("name", yhm);
		List<TreeNode> list =  xtglServer.getBmbmTreeByName(hm);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 编码编码树列表
	 */
	public void loadGhny(){
		List<TreeNode> list =  xtglServer.loadGhny();
		try {
			JsonUtils.write(list, getresponse().getWriter());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 编码编码树列表
	 */
	public void loadXmtjGhny(){
		List<TreeNode> list =  xtglServer.loadXmtjGhny();
		try {
			JsonUtils.write(list, getresponse().getWriter());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getWhTreeByNameFunds() {
		try {
			yhm=java.net.URLDecoder.decode(yhm,"UTF-8");
			HashMap<String, String> hm=new HashMap<String, String>();
			hm.put("name", yhm);
			List<TreeNode> list =  xtglServer.getWhTreeByNameFunds(hm);
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getWhTreeByName(){
		try {
			yhm=java.net.URLDecoder.decode(yhm,"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		HashMap<String, String> hm=new HashMap<String, String>();
		hm.put("name", yhm);
		List<TreeNode> list =  xtglServer.getWhTreeByName(hm);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void getFwqTime(){

		String nf=new SimpleDateFormat("yyyy").format(new Date());
        String yf=new SimpleDateFormat("MM").format(new Date());
        List l= new ArrayList<Xmjbxx>();
        /*for(int i=0;i<4;i++){
        	String yf2="";
        	Xmjbxx x=new Xmjbxx();
        	if(Integer.valueOf(yf)-i>0){
        		if(Integer.valueOf(yf)-i<10) yf2="0"+(Integer.valueOf(yf)-i);
        		else yf2=yf;
        		x.setTbsj(nf+"-"+yf2);
        		l.add(x);
        	}else{
        		yf2=Integer.valueOf(yf)+12-i+"";
        		else yf2=yf;
        		x.setTbsj((Integer.valueOf(nf)-1)+"-"+yf2);
        		l.add(x);
        	}
        }*/

        for(int i=0;i<24;i++){
        	String yf2="";
        	Xmjbxx x=new Xmjbxx();
        	if(Integer.valueOf(yf)-i>0){
        		if(Integer.valueOf(yf)-i<10) yf2="0"+(Integer.valueOf(yf)-i);
        		else yf2=""+(Integer.valueOf(yf)-i);
        		x.setTbsj(nf+"-"+yf2);
        		l.add(x);
        	}else{
        		if(Integer.valueOf(yf)+12-i>0){
	        		if(Integer.valueOf(yf)+12-i<10) yf2="0"+(Integer.valueOf(yf)+12-i);
        			else yf2=Integer.valueOf(yf)+12-i+"";

	        		x.setTbsj((Integer.valueOf(nf)-1)+"-"+yf2);
	        		l.add(x);
        		}
        		else{
        			if(Integer.valueOf(yf)+24-i<10) yf2="0"+(Integer.valueOf(yf)+24-i);
        			else yf2=Integer.valueOf(yf)+24-i+"";
            		x.setTbsj((Integer.valueOf(nf)-2)+"-"+yf2);
	        		l.add(x);
        		}
        	}
        }

		try {
			JsonUtils.write(l, getresponse().getWriter());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void getWhTreeByNameTj(){
		try {
			yhm=java.net.URLDecoder.decode(yhm,"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		HashMap<String, String> hm=new HashMap<String, String>();
		hm.put("name", yhm);
		List<TreeNode> list =  xtglServer.getWhTreeByNameTj(hm);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getWhTreeByNameShNew(){
		HashMap<String, String> hm=new HashMap<String, String>();
		hm.put("xmnf", xmnf);
		hm.put("gydwbm", gydwbm);
		List<TreeNode> list =  xtglServer.getWhTreeByNameShNew(hm);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getWhTreeByNameSh(){
		try {
			yhm=java.net.URLDecoder.decode(yhm,"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		HashMap<String, String> hm=new HashMap<String, String>();
		hm.put("name", yhm);
		List<TreeNode> list =  xtglServer.getWhTreeByNameSh(hm);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 插入单位
	 */
	public void insertDw(){
		boolean bl=xtglServer.insertDw(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}
	/**
	 * 插入单位
	 */
	public void deleteDwById(){
		boolean bl=xtglServer.deleteDwById(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}
	/**
	 * cha单位
	 */
	public void selectDwById(){
		List<Unit> list =  xtglServer.selectDwById(unit);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * update单位
	 */
	public void updateDw(){
		boolean bl=xtglServer.updateDw(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}
	/**
	 * bmbm树
	 */
	public void selectBmbmList(){
		int count = xtglServer.selectBmbmListCount(unit);
		List<Unit> list = xtglServer.selectBmbmList(unit);
		for(int i=0;i<list.size();i++){
			if(!unit.getId().equals(list.get(i).getId()))
			{
				list.get(i).set_parentId(list.get(i).getParent());
			}
			if(list.get(i).getId().length()==4){
				list.get(i).setState("closed");
			}
		}
		EasyUIPage<Unit> ep = new EasyUIPage<Unit>();
		ep.setTotal(count);
		ep.setRows(list);
		try {
			JsonUtils.write(ep, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * insertBmbm
	 */
	public void insertBmbm(){
		boolean bl=xtglServer.insertBmbm(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}
	/**
	 * updateBmbm
	 */
	public void updateBmbm(){
		boolean bl=xtglServer.updateBmbm(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}
	/**
	 * deleteBmbm
	 */
	public void deleteBmbmById(){
		boolean bl=xtglServer.deleteBmbmById(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void checkname(){
		HashMap hm=new HashMap();
		hm.put("truename", master.getTruename());
		List<Master> list=xtglServer.checkname(hm);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 特殊地区列表
	 */
	public void selectTsdqList(){
		List<Param> list=xtglServer.selectTsdqList(param);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void updateTsdqZt(){
		boolean bl=xtglServer.updateTsdqZt(param);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void selAllTsdq2() {
		try {
		    List<TreeNode> l=xtglServer.selAllTsdq2(yhdw);
            String s=JSONArray.fromObject(l).toString();
			ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void selAllTsdq() {
		try {
            List<TreeNode> l=xtglServer.selAllTsdq(yhdw);
		    String s=JSONArray.fromObject(l).toString();
            ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	public void checkXzqhCfById(){
		List<Unit> list=xtglServer.checkXzqhCfById(unit);
		try {
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void insertXzqh(){
		boolean bl=xtglServer.insertXzqh(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void updateXzqh(){
		boolean bl=xtglServer.updateXzqh(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void deleteXzqhById(){
		boolean bl=xtglServer.deleteXzqhById(unit);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void insertTsdq(){
		boolean bl=xtglServer.insertTsdq(param);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void deleteTsdqById(){
		boolean bl=xtglServer.deleteTsdqById(param);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void selectTsdqById(){
		Param p=xtglServer.selectTsdqById(param);
		try {
			JsonUtils.write(p, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void selectYhById(){
		Master m=xtglServer.selectYhById(master);
		try {
			JsonUtils.write(m, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void updateTsdq(){
		boolean bl=xtglServer.updateTsdq(param);
		if(bl == true){
			ResponseUtils.write(getresponse(), "true");
		}else{
			ResponseUtils.write(getresponse(), "false");
		}
	}

	public void selAllBm(){
		try {
            List<TreeNode> l=xtglServer.selAllBm(yhdw);
		    String s=JSONArray.fromObject(l).toString();
            ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void selAllBm2(){
		try {
            List<TreeNode> l=xtglServer.selAllBm2(yhdw);
		    String s=JSONArray.fromObject(l).toString();
            ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void selQxByUser(){
		List<Param> l=null;
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();

				l=xtglServer.selQxByUser(param);
				String qx1="";String qx2="";String qx3="";String qx4="";String qx5="";String qx6="";String qx7="";String qx8="";
				String qx9="";
				for (Param param : l) {
					if(param.getId().length()==4)
						qx1+=","+param.getId();
					if(param.getId().length()==6)
						qx2+=","+param.getId();
					if(param.getId().length()==8)
						qx3+=","+param.getId();
					if(param.getId().length()==10)
						qx4+=","+param.getId();
					if(param.getId().length()==12)
						qx5+=","+param.getId();
					if(param.getId().length()==14)
						qx6+=","+param.getId();
					if(param.getId().length()==16)
						qx7+=","+param.getId();
					if(param.getId().length()==18)
						qx8+=","+param.getId();
					if(param.getId().length()==20)
						qx9+=","+param.getId();
				}
				if(qx1.length()>0)
				session.setAttribute("qx1", qx1.substring(1));
				if(qx2.length()>0)
				session.setAttribute("qx2", qx2.substring(1));
				if(qx3.length()>0)
				session.setAttribute("qx3", qx3.substring(1));
				if(qx4.length()>0)
				session.setAttribute("qx4", qx4.substring(1));
				if(qx5.length()>0)
				session.setAttribute("qx5", qx5.substring(1));
				if(qx6.length()>0)
				session.setAttribute("qx6", qx6.substring(1));
				if(qx7.length()>0)
				session.setAttribute("qx7", qx7.substring(1));
				if(qx8.length()>0)
					session.setAttribute("qx8", qx8.substring(1));
				if(qx9.length()>0)
					session.setAttribute("qx9", qx9.substring(1));
				//List<Param> l1	=xtglServer.selQxByUser1(param);
				session.setAttribute("sour", l);


			JsonUtils.write(l, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	public void selQxByUser2(){
		List<Param> l=null;
		List<Param> l1=null;
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			if(session.getAttribute("sour")!=null){
				l=(List<Param>) session.getAttribute("sour");
			}else{
				l=xtglServer.selQxByUser(param);
				String qx1="";String qx2="";String qx3="";String qx4="";String qx5="";String qx6="";String qx7="";String qx8="";
				String qx9="";
				for (Param param : l) {
					if(param.getId().length()==4)
						qx1+=","+param.getId();
					if(param.getId().length()==6)
						qx2+=","+param.getId();
					if(param.getId().length()==8)
						qx3+=","+param.getId();
					if(param.getId().length()==10)
						qx4+=","+param.getId();
					if(param.getId().length()==12)
						qx5+=","+param.getId();
					if(param.getId().length()==14)
						qx6+=","+param.getId();
					if(param.getId().length()==16)
						qx7+=","+param.getId();
					if(param.getId().length()==18)
						qx8+=","+param.getId();
					if(param.getId().length()==20)
						qx9+=","+param.getId();
				}
				if(qx1.length()>0)
				session.setAttribute("qx1", qx1.substring(1));
				if(qx2.length()>0)
				session.setAttribute("qx2", qx2.substring(1));
				if(qx3.length()>0)
				session.setAttribute("qx3", qx3.substring(1));
				if(qx4.length()>0)
				session.setAttribute("qx4", qx4.substring(1));
				if(qx5.length()>0)
				session.setAttribute("qx5", qx5.substring(1));
				if(qx6.length()>0)
				session.setAttribute("qx6", qx6.substring(1));
				if(qx7.length()>0)
				session.setAttribute("qx7", qx7.substring(1));
				if(qx8.length()>0)
					session.setAttribute("qx8", qx8.substring(1));
				if(qx9.length()>0)
					session.setAttribute("qx9", qx9.substring(1));
				//l1=xtglServer.selQxByUser1(param);
				session.setAttribute("sour", l);
			};
		try {
			JsonUtils.write(l, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	public void selAllQx(){
		List<TreeNode> l=xtglServer.selQxByRoleid(param);
		TreeNode root = returnRoot(l,l.get(0),new ArrayList<Param>());
		List<TreeNode> children = root.getChildren();
		try {
		    String s=JSONArray.fromObject(children).toString();
            ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void selAllQx2(){
		List<TreeNode> l=xtglServer.selAllQx2(yhdw);
		try {
		    String s=JSONArray.fromObject(l).toString();
            ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void selQxByRoleid(){
		List<TreeNode> l=xtglServer.selQxByRoleid(param);
		List<Param> l2=xtglServer.selQxListByRoleid(param);


		TreeNode root = returnRoot(l,l.get(0),l2);
		List<TreeNode> children = root.getChildren();
		try{
		    String s=JSONArray.fromObject(children).toString();
            ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private TreeNode returnRoot(List<TreeNode> list, TreeNode zzjgTree,List<Param> l2){
		for (Param p : l2) {
				if(p.getSourceid().equals(zzjgTree.getId())) zzjgTree.setChecked("true");
		}

		for(TreeNode temp : list){
			if(temp!=zzjgTree){
				if(temp.getParent() != null &&temp.getParent() !="" && temp.getParent().equals(zzjgTree.getId())){
					zzjgTree.setState("closed");
					zzjgTree.getChildren().add(temp);
					returnRoot(list,temp,l2);
				}
			}
		}
		return zzjgTree;
	}


	public void checkJsCfByName(){
		List<Param> l=xtglServer.checkJsCfByName(param);
		try {
			JsonUtils.write(l, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void selJsUsedById(){
		List<Param> l=xtglServer.selJsUsedById(param);
		try {
			JsonUtils.write(l, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void selectlxbyxmid(){
		List<Lxsh> l=xtglServer.selectlxbyxmid(lxsh);
		try {
			JsonUtils.write(l, getresponse().getWriter());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}


	public String getYhdw() {
		return yhdw;
	}
	public void setYhdw(String yhdw) {
		this.yhdw = yhdw;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public Unit getUnit() {
		return unit;
	}
	public void setUnit(Unit unit) {
		this.unit = unit;
	}
	public Master getMaster() {
		return master;
	}
	public void setMaster(Master master) {
		this.master = master;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getYhm() {
		return yhm;
	}
	public void setYhm(String yhm) {
		this.yhm = yhm;
	}
	public String getYhzt() {
		return yhzt;
	}
	public void setYhzt(String yhzt) {
		this.yhzt = yhzt;
	}
	public String getYhids() {
		return yhids;
	}
	public void setYhids(String yhids) {
		this.yhids = yhids;
	}
	public String getGmgid() {
		return gmgid;
	}
	public void setGmgid(String gmgid) {
		this.gmgid = gmgid;
	}
	public String getYzm() {
		return yzm;
	}
	public void setYzm(String yzm) {
		this.yzm = yzm;
	}
	public Param getParam() {
		return param;
	}
	public void setParam(Param param) {
		this.param = param;
	}

	public Plan_flwbzbz getFlwbzbz() {
		return flwbzbz;
	}
	public void setFlwbzbz(Plan_flwbzbz flwbzbz) {
		this.flwbzbz = flwbzbz;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getAnyXml() {
		return anyXml;
	}
	public void setAnyXml(String anyXml) {
		this.anyXml = anyXml;
	}
	public void userlogin(){
		try {
			if(name==null||"".equals(name))
			return;
			else {
				List<Master> l = xtglServer.userlogin(name);
				JsonUtils.write(l, getresponse().getWriter());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void createMenu(){
		try {
			Unit unit=new Unit();

			String id = "";
			HttpSession session = getRequest().getSession();
				session.getAttribute("qx3");
					//id += c.getValue().replaceAll("%2C", ",") + ",";
				//System.out.println(session.getAttribute("qx2")+"--------");
				id=session.getAttribute("qx2")+","+session.getAttribute("qx3")+","+session.getAttribute("qx4")+","+session.getAttribute("qx5")+","+session.getAttribute("qx6")+","+session.getAttribute("qx7")+","+session.getAttribute("qx8")+","+session.getAttribute("qx9");
			//System.out.println(id);
			if(",".equals(id.substring(id.length()-1))){
				id=id.substring(0,id.length()-1);
			}
			unit.setParent(parent);
			unit.setId(id);
			List<TreeNode> list = xtglServer.createMenu(unit);
			if("0104".equals(parent)){
				/*TreeNode root = returnRoot(list,list.get(0));
				List<TreeNode> children = root.getChildren();*/
			    String s=JSONArray.fromObject(list).toString();
	            ResponseUtils.write(getresponse(), s);
			}else{
				TreeNode root = returnRoot(list,list.get(0));
				List<TreeNode> children = root.getChildren();
			    String s=JSONArray.fromObject(children).toString();
	            ResponseUtils.write(getresponse(), s);
			}


		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private TreeNode returnRoot(List<TreeNode> list, TreeNode zzjgTree){

		for(TreeNode temp : list){

			if(temp!=zzjgTree){
				if(temp.getParent() != null &&temp.getParent() !="" && temp.getParent().equals(zzjgTree.getId())){
					//zzjgTree.setState("closed");
					zzjgTree.getChildren().add(temp);
					returnRoot(list,temp);
				}
			}
		}
		return zzjgTree;
	}

	public void getQxfromSession(){
		try {
		HttpSession session = getRequest().getSession();
		ResponseUtils.write(getresponse(), (String)session.getAttribute(qx));
			//JsonUtils.write((String)session.getAttribute(qx), getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}


	}
	private String qx;

	public String getQx() {
		return qx;
	}
	public void setQx(String qx) {
		this.qx = qx;
	}


	public void urllogin(){
		MessageDigest md5;
		String temp="";
		String sKey="zkrhdsxjx_";

		try {
			temp=URLDecoder.decode(master.getTruename(),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*String a="ZwsUcorZkCrsujLiL6T2vQ==";
		System.out.println(DigestUtils.md5Hex(sKey+a));
		System.out.println(master.getPassword());*/
		HashMap hm=new HashMap();
		hm.put("truename", temp);
		Master m=xtglServer.selectPwd(temp);
		if(m==null){
			ResponseUtils.write(getresponse(), "false");
		}else{
			String npwd=DigestUtils.md5Hex(sKey+m.getPassword());//通过后台取出的密码进行加密
			if(master.getPassword().equals(npwd)){
				hm.put("password", m.getPassword());
				HashMap<String,String> bl = xtglServer.login(hm);
				if(bl!=null){
					HttpServletRequest request = ServletActionContext.getRequest();
					HttpSession session = request.getSession();
					session.setAttribute("truename", bl.get("TRUENAME"));
					try {
						JsonUtils.write(bl, getresponse().getWriter());
					} catch (Exception e1) {
						e1.printStackTrace();
 						}
				}else{
					ResponseUtils.write(getresponse(), "false");
				}
			}else{
				ResponseUtils.write(getresponse(), "false");
			}
		}

	}


	public void getbmbmbyid(){
			try {
				Unit u = xtglServer.getbmbmbyid(unit);
				JsonUtils.write(u, getresponse().getWriter());
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	public void getjhxmjd() {
		try {
		List<TreeNode> list=null;
		list=xtglServer.getjhxmjd();
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void getxmmc() {
		try {
		List<TreeNode> list=null;
		list=xtglServer.getxmmc();
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getTsdq() {
		try {
		List<?> list=null;
		if (!MbCacheUtil.exists("tsdqList")) {
			list = xtglServer.getTsdq();
			MbCacheUtil.put("tsdqList", list);
		}
		list = MbCacheUtil.get("tsdqList");
		JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void getjhxdwh(){
//		System.out.println("1111");
		try {
			String tablename="";
			List<TreeNode> list=null;
			if("gs_gsdgz".equals(type)){
				tablename="and (xmid like '__________1___' or xmid like '__________2___' or xmid like '__________3___')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("gs_yhdzx".equals(type)){
				tablename="and (xmid like '__________4___')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("gs_zhhf".equals(type)){
				tablename="and (xmid like '__________5___')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("gs_lwjggz".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='路网结构改造' and gydwdm like '2%')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("nc_lwjggz".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='路网结构改造' and gydwdm like '1%')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if ("gs_cz".equalsIgnoreCase(type)) {
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='场站' and gydwdm like '2%')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("nc_gljs".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='农村公路建设')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("nc_xzxm".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='农村公路新增项目')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("gs_xzxm".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='专项整治')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if ("nc_gdbn".equals(type)) {
				tablename = "and xmid in(select xmbm from xmjbxx where jsxz='改渡便民')";
				list = xtglServer.getjhxdwh(tablename);
			}
			if ("nc_xjql".equalsIgnoreCase(type)) {
				tablename = "and xmid in(select xmbm from xmjbxx where jsxz='新改建桥梁')";
				list = xtglServer.getjhxdwh(tablename);
			}
			if("gs_db".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='国省道新增项目')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("nc_tzrc".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='通自然村')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("nc_yhgc".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='养护工程')";
				list=xtglServer.getjhxdwh(tablename);
			}

			if("gs_all".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx)";
				list=xtglServer.getjhxdwh(tablename);
			}

			if("gs_all_bb".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx)";
				list=xtglServer.getjhxdwhbb(tablename);
			}
			if("gs_sfl".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='示范路')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("gs_fwq".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='服务区')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("gs_btcjcd".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='不停车检测点')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("gs_yhzx".equals(type)){
				tablename="and xmid in(select xmbm from xmjbxx where jsxz='养护中心')";
				list=xtglServer.getjhxdwh(tablename);
			}
			if("sbjhzjtj".equals(type)){
				list=xtglServer.getjhxdwhSbzj();
			}

			/*if(("wqgz").equals(unit.getXzqhdm())){
				tablename="select distinct z.jhxdwh id,z.jhxdwh text from plan_wqgz p left join plan_zjxd z on p.id=z.xmid where z.jhxdwh is not null";
				list=xtglServer.setjhxdwh1(tablename);
			}
			if(("afgc").equals(unit.getXzqhdm())){
				tablename="select distinct z.jhxdwh id,z.jhxdwh text from plan_af p left join plan_zjxd z on p.id=z.xmid where z.jhxdwh is not null";
				list=xtglServer.setjhxdwh1(tablename);
			}
			if(("zhfz").equals(unit.getXzqhdm())){
				tablename="select distinct z.jhxdwh id,z.jhxdwh text from plan_zhfz p left join plan_zjxd z on p.id=z.xmid where z.jhxdwh is not null";
				list=xtglServer.setjhxdwh1(tablename);
			}*/
			int i=1;
			for (TreeNode treeNode : list) {
				treeNode.setId(i+"");
				treeNode.setIconCls("icon-none");
				i++;
			}
			JsonUtils.write(list, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void selAllXzqh1(){
		String key = "xzqh" + yhdw.replaceAll("0*$", "");
		if (!MbCacheUtil.exists(key)) {
			List<TreeNode> xzqhtreelist = xtglServer.selAllXzqh1(yhdw.replaceAll("0*$",""));
			List list = MbCacheUtil.queryCacheTreeData(xzqhtreelist);
			MbCacheUtil.put(key, list);
		}
		List<?> result = MbCacheUtil.get(key);
		try{
			String s = JSONArray.fromObject(result).toString();
			ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*TreeNode root = returnRoot1(l,l.get(0));
		List<TreeNode> children1 = new ArrayList<TreeNode>();
		children1.add(l.get(0));
		List<TreeNode> children = root.getChildren();
		children1.get(0).setChildren(children);*/
	}
	public void selAllUnit1(){
		String key = "gydw" + yhdw.replaceAll("0*$", "");
		if (!MbCacheUtil.exists(key)) {
			List<TreeNode> l = xtglServer.selAllUnit1(yhdw.replaceAll("0*$",""));
			List list = MbCacheUtil.queryCacheTreeData(l);
			MbCacheUtil.put(key, list);
		}
		List<?> result = MbCacheUtil.get(key);
		try {
			String s = JSONArray.fromObject(result).toString();
			ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*String s=JSONArray.fromObject(result).toString();
		TreeNode root = returnRoot1(l,l.get(0));
		List<TreeNode> children1 = new ArrayList<TreeNode>();
		children1.add(l.get(0));
		List<TreeNode> children = root.getChildren();
		children1.get(0).setChildren(children);
		try{
			String s=JSONArray.fromObject(children1).toString();
            ResponseUtils.write(getresponse(), s);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}
	private TreeNode returnRoot1(List<TreeNode> list, TreeNode zzjgTree){
		for(TreeNode temp : list){
			if(temp!=zzjgTree){
				if(temp.getParent() != null &&temp.getParent() !="" && temp.getParent().equals(zzjgTree.getId())){
					if(zzjgTree.getId().length()==6){
						if(zzjgTree.getId().substring(2).equals("0000")){
							zzjgTree.setState("open");
						}else{
							zzjgTree.setState("closed");
						}
					}
					else if(zzjgTree.getId().length()==11){
						if(zzjgTree.getId().substring(7).equals("0000")){
							zzjgTree.setState("open");
						}else{
							zzjgTree.setState("closed");
						}
					}else{
						zzjgTree.setState("closed");
					}
					zzjgTree.getChildren().add(temp);
					returnRoot1(list,temp);
				}
			}
		}
		return zzjgTree;
	}


	public void exportBb_set(){
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			session.setAttribute("gydwbb", gydw);
			session.setAttribute("xzqhbb", xzqh);
			session.setAttribute("sql", sql);
			session.setAttribute("nameValue", nameValue);
			session.setAttribute("colValue", colValue);
			session.setAttribute("nf", nf);
			session.setAttribute("yf", yf);

			ResponseUtils.write(getresponse(), ""+true);
			//JsonUtils.write(true, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void getxqxbyid() {
		try {
			JsonUtils.write(xtglServer.getxqxbyid(param), getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void selectJcsmSl() {
		try {
			JsonUtils.write(xtglServer.selectJcsmSl(MyUtil.getQueryDwTJ(gydw, "xm.gydwdm")), getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addBbTj() {
		Xmjbxx xmjbxx=new Xmjbxx();
		xmjbxx.setGydw(gydw);xmjbxx.setBbmc(bbmc);xmjbxx.setYf(yf);xmjbxx.setTsdq(truename);
		ResponseUtils.write(getresponse(), ""+xtglServer.addBbTj(xmjbxx));
	}

	public void getFwParam() {
		try {
			Fwparam f = new Fwparam();
			String ip=getIpAddress(getRequest());
			f.setIp(ip);
			//System.out.println(getMacAddrByIp(ip));
			//
			if(username==null) return;
			f.setUsername(username);
			f.setMac(getLocalMac(ip));
			HttpClient http=new HttpClient();
			if(f.getMac()==null||"".equals(f.getMac())) {
				f.setMac(http.getMACAddress());
			}
			f.setSyscode("B05");
			f.setSystype("BS");
			f.setTime(new SimpleDateFormat("yyyy/MM/dd hh24:mm:ss").format(new Date()));
			String url="";
			if("1".equals(type)) {
				 url="http://36.2.11.89:9999/dlzt/insertLoginUser";
			}
			if("2".equals(type)) {
				 url="http://36.2.11.89:9999/dlzt/insertLogRefUser";
			}
			if("3".equals(type)) {
				 url="http://36.2.11.89:9999/dlzt/insertLogoutUser";
			}
			//String re=sendPost(url, JSON.toJSONString(f));

			HashMap<String,Object> map=new HashMap();
			map.put("syscode",f.getSyscode());
			map.put("systype",f.getSystype());
			map.put("mac",f.getMac());
			map.put("username",f.getUsername());
			map.put("ip",f.getIp());
			JSONObject re=http.doPostHttp(url,map);
			JsonUtils.write(f, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// todo 请求远程接口，返回数据体
	public static void main(String[] args) {
		String str = "POLYGON((115.75765104987603 28.488374309558925))";
		System.out.println(str.replaceAll("\\s", "+"));
//		HttpClient http = new HttpClient();
//		String polygon = "POLYGON((115.75765104987603+28.488374309558925,115.75765104987603+28.53457817310446,115.80385491342157+28.53457817310446,115.80385491342157+28.488374309558925,115.75765104987603+28.488374309558925))";
//		String newUrl = "http://36.2.6.35:7003/geoserver-pg/back1/rest/search?params=%7B%22layerName%22:%22view_by_line%22,%22spatialFilter%22:%22" + polygon +"%22,%22filter%22:%22roadcode+like+%27G%25%27+or+roadcode+like+%27S%25%27%22,%22pager%22:%7B%7D,%22isReturnGeometry%22:%22false%22,%22spatialRel%22:%22INTERSECTS%22,%22orderByFields%22:%22order+by+sort1,+sort2,+lxbm+asc%22%7D";
//
//		JSONObject re=http.doPostHttp(newUrl,null);
//		System.out.println(re);
	}

	public void queryMapByPolygon() {
		try {
			HttpClient http = new HttpClient();
			String poly = this.getPolygon().replaceAll("\\s", "+");
			// String polygon = "POLYGON((115.75765104987603+28.488374309558925,115.75765104987603+28.53457817310446,115.80385491342157+28.53457817310446,115.80385491342157+28.488374309558925,115.75765104987603+28.488374309558925))";
			String newUrl = "http://36.2.6.35:7003/geoserver-pg/back1/rest/search?params=%7B%22layerName%22:%22view_by_line%22,%22spatialFilter%22:%22" + poly +"%22,%22filter%22:%22roadcode+like+%27G%25%27+or+roadcode+like+%27S%25%27%22,%22pager%22:%7B%7D,%22isReturnGeometry%22:%22false%22,%22spatialRel%22:%22INTERSECTS%22,%22orderByFields%22:%22order+by+sort1,+sort2,+lxbm+asc%22%7D";
			System.out.println(newUrl);
			JSONObject re=http.doPostHttp(newUrl,null);
			System.out.println(re);
			JsonUtils.write(re, getresponse().getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
     * 根据IP地址获取mac地址
     * @param ipAddress 127.0.0.1
     * @return
     * @throws SocketException
     * @throws UnknownHostException
     */
public static String getLocalMac(String ipAddress) throws SocketException,
UnknownHostException {
    // TODO Auto-generated method stub
    String str = "";
    String macAddress = "";
    final String LOOPBACK_ADDRESS = "127.0.0.1";
    // 如果为127.0.0.1,则获取本地MAC地址。
    if (LOOPBACK_ADDRESS.equals(ipAddress) ||"192.168.0.120".equals(ipAddress)) {
        InetAddress inetAddress = InetAddress.getLocalHost();
        // 貌似此方法需要JDK1.6。
        byte[] mac = NetworkInterface.getByInetAddress(inetAddress)
            .getHardwareAddress();
        // 下面代码是把mac地址拼装成String
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            /*if (i != 0) {
                sb.append("-");
            }*/
            // mac[i] & 0xFF 是为了把byte转化为正整数
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length() == 1 ? 0 + s : s);
        }
        // 把字符串所有小写字母改为大写成为正规的mac地址并返回
        macAddress = sb.toString().trim().toUpperCase();
        return macAddress;
    } else {
        // 获取非本地IP的MAC地址
        try {
            Process p = Runtime.getRuntime()
                .exec("nbtstat -A " + ipAddress);
            InputStreamReader ir = new InputStreamReader(p.getInputStream());

            BufferedReader br = new BufferedReader(ir);

            while ((str = br.readLine()) != null) {
                if(str.indexOf("MAC")>1){
                    macAddress = str.substring(str.indexOf("MAC")+9, str.length());
                    macAddress = macAddress.trim();
                    break;
                }
            }
            p.destroy();
            br.close();
            ir.close();
        } catch (IOException ex) {
        	ex.printStackTrace();
        }
        return macAddress;
    }
}

    public String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-real-ip");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
}

    public String getMacAddrByIp(String ip) {
        String macAddr = null;
        try {
            Process process = Runtime.getRuntime().exec("nbtstat -a " + ip);
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            Pattern pattern = Pattern.compile("([A-F0-9]{2}-){5}[A-F0-9]{2}");
            Matcher matcher;
            for (String strLine = br.readLine(); strLine != null;
                 strLine = br.readLine()) {
                matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    macAddr = matcher.group();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return macAddr;
    }


    public String getMACAddressByIp(String ip){
        String str = "";
        String macAddress = "";
        try {
            Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
            InputStreamReader ir = new InputStreamReader(p.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (int i = 1; i < 100; i++) {
                str = input.readLine();
                if (str != null) {
                    if (str.indexOf("MAC Address") > 1) {
                        macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return macAddress;
    }




    /**
	 * 向指定 URL 发送POST方法的请求
	 *
	 * @param url
	 *            发送请求的 URL
	 * @param param
	 *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
	 * @return 所代表远程资源的响应结果
	 */
	public String sendPost(String url, String param) {
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(conn.getOutputStream());
			// 发送请求参数
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送 POST 请求出现异常！" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输出流、输入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}



}
